package cc.lucien.jdbcDemo01;

import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

/**
 * 数据库操作工具类
 */
public class DButils {

    private static String driver;
    private static String url;
    private static String username;
    private static String password;

    private static Connection connection;
    private static PreparedStatement preparedStatement;

    /**
     * 静态块只会在类加载的时候执行一次，
     * 加载驱动类也主需要进行一次，
     * 所以把读取配置文件和加载驱动类写在静态块中
     */
    static {
        try {
            //加载配置文件
            Properties properties = new Properties();
            InputStream inputStream = DButils.class.getResourceAsStream("db.properties");
            properties.load(inputStream);
            //赋值
            driver = properties.getProperty("driver");
            url = properties.getProperty("url");
            username = properties.getProperty("username");
            password = properties.getProperty("password");

            //加载驱动类
            Class.forName(driver);
        } catch (IOException e) {
            System.err.println("加载配置文件失败");
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            System.err.println("加载驱动类失败！");
            e.printStackTrace();
        }

    }


    /**
     * 获取数据库连接
     * @return
     */
    public static Connection getConnection() {
        try {
            connection = DriverManager.getConnection(url, username, password);
            return connection;
        } catch (SQLException e) {
            System.err.println("获取数据库连接失败！");
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 查询方法
     * @param sql
     * @param parms
     * @return
     */
    public static ResultSet executeQuery(String sql, Object...parms) {
        try {
            //TODO 数据有效性判断
            //获取连接
            getConnection();
            //预处理 sql
            preparedStatement = connection.prepareStatement(sql);
            //设置值
            setParams(parms);
            //执行并返回结果
            return preparedStatement.executeQuery();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static int executeUpdate(String sql, Object... parms) {
        try {
            //获取连接
            getConnection();
            //预处理sql
            preparedStatement = connection.prepareStatement(sql);
            //赋值
            setParams(parms);
            //执行并返回结果
            return preparedStatement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return -1;
    }

    /**
     * 关闭连接，释放资源
     */
    public static void close(){
        try {
            if (preparedStatement != null && !preparedStatement.isClosed()){
                preparedStatement.close();
                preparedStatement = null;
            }
            if (connection != null && !connection.isClosed()) {
                connection.close();
                connection = null;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    private static void setParams(Object...parms) {
        if (parms != null) {
            for (int i = 0; i < parms.length; i++){
                try {
                    preparedStatement.setObject(i + 1, parms[i]);
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
